import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";
import type { SotrageClassList, StorageClass } from "./types";
import { k8sPath, type MergePathReq } from "./apiHelper";

export class K8sStorageClass {
  scUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/storage.k8s.io/v1/storageclasses`;
  };

  baseReq: MergePathReq = {
    src: "storageclasses",
    app: "storage.k8s.io/v1"
  };

  listStorageClasses(req: MergePathReq): BaseResponse<SotrageClassList> {
    const api = k8sPath({
      ...req,
      ...this.baseReq
    });
    return http.get(api);
  }

  deleteStorageClasses(req: MergePathReq) {
    const api = k8sPath({
      ...req,
      ...this.baseReq,
      srcName: req.data.metadata.name
    });

    return http.delete(api);
  }

  getStorageClass(req: MergePathReq<StorageClass>) {
    const api = k8sPath({
      ...req,
      src: "storageclasses",
      namespace: req.data.metadata.namespace,
      srcName: req.data.metadata.name
    });
    return http.get(api);
  }

  createStorageClass(req: MergePathReq, data: StorageClass) {
    const api = k8sPath({
      ...req,
      ...this.baseReq
    });
    return http.post(api, { data });
  }

  updateStorageClass(req: MergePathReq, data: StorageClass) {
    const api = k8sPath({
      ...req,
      ...this.baseReq,
      srcName: req.data.metadata.name,
      namespace: req.data.metadata.namespace
    });
    return http.put(api, { data });
  }

  changeStorageClass(req: MergePathReq, data: StorageClass) {
    const api = k8sPath({
      ...req,
      ...this.baseReq,
      srcName: req.data.metadata.name,
      namespace: req.data.metadata.namespace
    });
    return http.patch(api, { data });
  }
}
